package com.ruoyi.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sales.domain.*;
import com.ruoyi.sales.mapper.ZcStoreInfoMapper;
import com.ruoyi.sales.mapper.ZcStoreProductMapper;
import com.ruoyi.sales.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.sales.mapper.ZcSalesProductsMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售产品Service业务层处理
 *
 * @author woo
 * @date 2024-04-09
 */
@Service
public class ZcSalesProductsServiceImpl extends ServiceImpl<ZcSalesProductsMapper, ZcSalesProducts> implements IZcSalesProductsService {
    @Autowired
    private ZcSalesProductsMapper zcSalesProductsMapper;
    @Autowired
    private IZcSalesListService salesListService;
    @Autowired
    private ZcStoreProductMapper zcStoreProductMapper;
    @Autowired
    private IZcStoreEnterService storeEnterService;
    @Autowired
    private IZcStoreInfoService storeInfoService;

    /**
     * 查询销售产品
     *
     * @param productId 销售产品主键
     * @return 销售产品
     */
    @Override
    public ZcSalesProducts selectZcSalesProductsByProductId(Long productId) {
        return zcSalesProductsMapper.selectZcSalesProductsByProductId(productId);
    }

    @Override
    public List<ZcSalesProducts> selectZcSalesProductsList(ZcSalesProducts zcSalesProducts, Long salesId) {
        QueryWrapper<ZcSalesProducts> qw = new QueryWrapper<>();
        qw.eq("del",0).eq("sales_id",salesId);
        List<ZcSalesProducts> salesProductsList = zcSalesProductsMapper.selectList(qw);
        return salesProductsList;
    }

    /**
     * 查询销售产品列表
     *
     * @param zcSalesProducts 销售产品
     * @return 销售产品
     */
//    @Override
//    public List<ZcSalesProducts> selectZcSalesProductsList(ZcSalesProducts zcSalesProducts) {
//        return zcSalesProductsMapper.selectZcSalesProductsList(zcSalesProducts);
//    }

    /**
     * 新增销售产品
     *
     * @param zcSalesProducts 销售产品
     * @return 结果
     */
    @Override
    public int insertZcSalesProducts(ZcSalesProducts zcSalesProducts) {
                zcSalesProducts.setCreateTime(DateUtils.getNowDate());
                zcSalesProducts.setDel((long) 0);
//        ZcSalesList zcSalesList = salesListService.selectZcSalesListBySalesId(zcSalesProducts.getSalesId());
//        zcSalesProducts.setSalesProductNumber(zcSalesList.getSalesNumber());
        zcSalesProducts.setSalesProductNumber(generateLongUUID());
        return zcSalesProductsMapper.insertZcSalesProducts(zcSalesProducts);
    }

    public static long generateLongUUID() {
        UUID uuid = UUID.randomUUID();
        long mostSignificantBits = uuid.getMostSignificantBits();
        // 通过右移一位确保最高位是0（正数）
        long positiveLong = mostSignificantBits >>> 1;
        // 如果需要避免0值，可以加上一个偏移量
        positiveLong += 1; // 或者其他适当的偏移量
        return positiveLong;
    }

    /**
     * 修改销售产品
     *
     * @param zcSalesProducts 销售产品
     * @return 结果
     */
    @Override
    public int updateZcSalesProducts(ZcSalesProducts zcSalesProducts) {
                zcSalesProducts.setUpdateTime(DateUtils.getNowDate());
        return zcSalesProductsMapper.updateZcSalesProducts(zcSalesProducts);
    }

    /**
     * 批量删除销售产品
     *
     * @param productIds 需要删除的销售产品主键
     * @return 结果
     */
    @Override
    public int deleteZcSalesProductsByProductIds(Long[] productIds) {
        return zcSalesProductsMapper.deleteZcSalesProductsByProductIds(productIds);
    }

    /**
     * 删除销售产品信息
     *
     * @param productId 销售产品主键
     * @return 结果
     */
    @Override
    public int deleteZcSalesProductsByProductId(Long productId) {
        return zcSalesProductsMapper.deleteZcSalesProductsByProductId(productId);
    }

    @Override
    public List<ZcSalesProducts> selectAll() {
        QueryWrapper<ZcSalesProducts> qw = new QueryWrapper<>();
        qw.eq("del",0);
        List<ZcSalesProducts> productsList = zcSalesProductsMapper.selectList(qw);
        return productsList;
    }

    @Transactional
    @Override
    public Long checkStoreProducts(ZcSalesProducts zcSalesProducts) {

        //销售产品所需米数
        Long meters = Long.valueOf(zcSalesProducts.getMeters());

        QueryWrapper<ZcStoreProduct> qw = new QueryWrapper<>();
        qw.eq("del",0);
        //所有成品仓库对象
        List<ZcStoreProduct> storeProducts = zcStoreProductMapper.selectList(qw);
        //遍历找到每个入库单据
        for (ZcStoreProduct storeProduct : storeProducts) {
            //对应入库单据
            ZcStoreEnter zcStoreEnter = storeEnterService.selectZcStoreEnterByEnterId(storeProduct.getEnterId());
            //找到对应产品
            if (zcSalesProducts.getProductNumber()==zcStoreEnter.getProductId()){
                Long enterMeters = Long.valueOf(zcStoreEnter.getMeters());
                ZcStoreInfo zcStoreInfo = storeInfoService.selectZcStoreInfoByStoreId(zcStoreEnter.getStoreId());
                if (meters==0){
                    break;
                }else if (meters>=enterMeters){
                    meters -= enterMeters;
                    zcStoreInfo.setUntrueCount(enterMeters);
                    zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()-enterMeters);
                }else {
                    zcStoreInfo.setUntrueCount(meters);
                    zcStoreInfo.setTrueCount(zcStoreInfo.getTrueCount()-meters);
                    meters = enterMeters - meters;
                }
                storeInfoService.updateZcStoreInfo(zcStoreInfo);
            }
        }
        return meters;
    }
}